package com.cy.ppj.web.front.user;

import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.product.AdmissionAO;
import com.cy.ppj.model.doc.AdmissionLog;
import com.cy.ppj.service.product.AdmissionService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 准入查询
 * @author: Ocean
 * @create: 2018-08-09 10:16
 **/
@Slf4j
@Service
@Api(tags = "准入接口")
@RestController
public class AdmissionController extends BaseController {

    @Autowired
    private AdmissionService admissionService;

    @ApiOperation("准入查询")
    @GetMapping(value = {"/admission", "/admin/admission"})
    public Result admission(@Validated AdmissionAO admissionAO, BindingResult bindingResult, HttpServletRequest request) {

        this.checkError(bindingResult);

        boolean idAdmin = isAdmin(request);

        //查询历史查询数据，查询有效期内的
        AdmissionLog admissionLog = admissionService.queryLogInCache(admissionAO.getIdNo());
        if (admissionLog != null) {
            return ok(idAdmin ? admissionLog.getContent() : admissionLog.getMessage());
        }

        AdmissionLog insertLog = admissionService.admission(admissionAO);

        admissionService.insertLog(insertLog);

        return ok(idAdmin ? insertLog.getContent() : insertLog.getMessage());
    }

    private boolean isAdmin(HttpServletRequest request) {

        return request.getRequestURI().contains("/admin/");
    }

}
